<?php

declare(strict_types=1);

namespace AdminBundle\Http\Controllers\Admin\V1;

use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\PostMapping;
use Throwable;
use AdminBundle\Services\RolesService;
use App\Exception\BusinessException;
use App\Exception\BussniessException;

/**
 * RolesController
 * 角色控制器
 * @package App\Controller\Admin
 */
class RolesController extends AbstractController
{
    /**
     * @Inject
     * @var RolesService
     */
    public $rolesService;

    /**
     * index
     * 角色列表，角色管理
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function index()
    {
        $page = (int) $this->request->query('page', 1);
        $pageSize = (int) $this->request->query('page_size', 20);

        $list = $this->rolesService->getList([], $page, $pageSize);

        return $this->response->success($list);
    }

    /**
     * store
     * 保存，新建、编辑都用该方法，区别是否有主键id
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function store()
    {
        try {
            $reqParam = $this->request->all();
            $result = $this->rolesService->saveRoles($reqParam);

            return $this->response->success($result['id']);
        } catch (Throwable $throwable) {
            throw new BusinessException(ErrorCode::SERVER_ERROR, '失败');
        }
    }

    /**
     * getInfo
     * 根据id获取单条记录信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     * @PostMapping(path="get_info")
     */
    public function getInfo($id)
    {
        $info = $this->rolesService->getInfo($id);

        return $this->response->success($info);
    }

    /**
     * destroy
     * 删除角色
     * @return \Psr\Http\Message\ResponseInterface
     *
     * @PostMapping(path="delete")
     */
    public function destroy($id)
    {
        $this->rolesService->deleteInfo($id);

        return $this->response->success('ok');
    }

    /**
     * getPermissions
     * 获取绑定权限信息
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function getPermissions($id)
    {
        $data = $this->rolesService->getRolePermissions($id);

        return $this->response->success($data);
    }

    /**
     * savePermissions
     * 保存角色权限
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function savePermissions()
    {
        $reqParam = $this->request->all();

        $this->rolesService->saveRolesPermissions($reqParam);

        return $this->response->success('ok');
    }

    /**
     * getUsers
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function getUsers()
    {
        $page = (int) $this->request->query('page', 1);
        $pageSize = (int) $this->request->query('page_size', 20);

        $role_id = $this->request->input('role_id');
        $inputData = [];
        if ($role_id) {
            $inputData['system_role_id'] = $role_id;
        }
        $list = $this->rolesService->getUsers($inputData, $page, $pageSize);

        return $this->response->success($list);
    }

    /**
     * searchUser
     * 角色添加用户时搜索用户
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function searchUser()
    {
        $reqParam = $this->request->all();
        $list = $this->rolesService->searchUser($reqParam);

        return $this->response->success($list);
    }

    /**
     * saveUser
     * 为角色添加成员
     * @return \Psr\Http\Message\ResponseInterface
     *
     */
    public function saveUser()
    {
        $reqParam = $this->request->all();
        if (isset($reqParam['user_id']) && isset($reqParam['role_id'])) {
            $saveData['system_role_id'] = $reqParam['role_id'];
            $saveData['user_id'] = $reqParam['user_id'];
            $this->rolesService->saveRolesUser($saveData);
        }

        return $this->response->success('ok');
    }

    /**
     * removeUser
     * 为角色移除用户
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function removeUser()
    {
        $inputData = $this->request->all();
        if (isset($inputData['user_id']) && isset($inputData['role_id'])) {
            $where['system_role_id'] = $inputData['role_id'];
            $where['user_id'] = $inputData['user_id'];
            $this->rolesService->deleteRolesUser($where);
        } else {
            throw new BussniessException(ErrorCode::PARAMETER_ERROR, '请求参数错误！');
        }

        return $this->response->success('ok');
    }
}